package com.vanky.graph.service.impl;


import com.vanky.graph.pojo.fqResult;
import com.vanky.graph.properties.HugeClientProperties;
import com.vanky.graph.service.AccurateQueryService;
import com.vanky.graph.utils.JsonUtil;
import org.apache.hugegraph.driver.GremlinManager;
import org.apache.hugegraph.driver.HugeClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AccurateQueryServiceImpl implements AccurateQueryService {

    @Autowired
    private HugeClientProperties hugeClientProperties;

    /*
     * 精确查询所有节点及两层节点
     */
    @Override
    public List<fqResult> queryAll(String word) {

        //连接数据库
        HugeClient hugeClient = HugeClient.builder(hugeClientProperties.getUrl(), hugeClientProperties.getGraph()).build();

        //获取gremlin管理器
        GremlinManager gremlin = hugeClient.gremlin();

        //执行gremlin语句进行查询
        List<Object> data = gremlin.gremlin("g.V().where(__.properties().hasValue('" + word + "')).as('startNode')\n" +
                ".bothE().as('edgeToMiddle').otherV().as('middleNode')\n" +
                ".bothE().as('edgeToEnd').otherV().as('endNode')\n" +
                ".select('startNode',  'edgeToMiddle','middleNode', 'edgeToEnd', 'endNode')").execute().data();

        //关闭服务器连接
        hugeClient.close();

        //返回json格式的结果
        return JsonUtil.toJson(data, fqResult.class);
    }

    /*
     * 精确查询某个类型的节点及两层节点，通用方法
     */
    public List<fqResult> queryType(String word,String label,String type ) {

        //连接数据库
        HugeClient hugeClient = HugeClient.builder(hugeClientProperties.getUrl(), hugeClientProperties.getGraph()).build();

        //获取gremlin管理器
        GremlinManager gremlin = hugeClient.gremlin();

        //执行gremlin语句进行查询
        List<Object> data = gremlin.gremlin("g.V().hasLabel('" + label + "').has('" + type + "').where(__.properties().hasValue('" + word + "')).as('startNode')\n" +
                ".bothE().as('edgeToMiddle').otherV().as('middleNode')\n" +
                ".bothE().as('edgeToEnd').otherV().as('endNode')\n" +
                ".select('startNode',  'edgeToMiddle','middleNode', 'edgeToEnd', 'endNode')").execute().data();

        //关闭服务器连接
        hugeClient.close();

        //返回json格式的结果
        return JsonUtil.toJson(data, fqResult.class);
    }

    /*
     * 精确查询法庭节点及两层节点
     */
    @Override
    public List<fqResult> queryCourt(String word) {
        List<fqResult> results = queryType(word,"slice_court","court_content");
        return results;
    }

    /*
     * 精确查询法官节点及两层节点
     */
    @Override
    public List<fqResult> queryJudge(String word) {
        List<fqResult> results = queryType(word,"slice_judge","judge_content");
        return results;
    }

    /*
     * 精确查询案由节点及两层节点
     */
    @Override
    public List<fqResult> queryCauseType(String word) {
        List<fqResult> results = queryType(word,"slice_cause","cause_type");
        return results;
    }

    /*
     * 精确查询法条节点及两层节点
     */
    @Override
    public List<fqResult> queryLaw(String word) {
        List<fqResult> results = queryType(word,"slice_law","law_content");
        return results;
    }

    /*
     * 精确查询律所节点及两层节点
     */
    @Override
    public List<fqResult> queryLawFirm(String word) {
        List<fqResult> results = queryType(word,"slice_lawfirm","lawfirm_content");
        return results;
    }

    /*
     * 精确查询律师节点及两层节点
     */
    @Override
    public List<fqResult> queryLawyer(String word) {
        List<fqResult> results = queryType(word,"slice_lawyer","lawyer_content");
        return results;
    }

    /*
     * 精确查询被告节点及两层节点
     */
    @Override
    public List<fqResult> queryLitigant(String word) {
        List<fqResult> results = queryType(word,"slice_litigant","litigant_content");
        return results;
    }

    /*
     * 精确查询案件节点及两层节点
     */
    @Override
    public List<fqResult> queryCase(String word) {
        List<fqResult> results = queryType(word,"slice_case","case_name");
        return results;
    }

}
